<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Kategori extends CI_Controller {
	private $limit=10;

	function __construct(){
		parent::__construct();
		
		$index	= $this->config->item('index_page');
		$host	= $this->config->item('base_url');

		$this->url = empty($index) ? $host : $host . $index . '/';
		$this->user	= unserialize(base64_decode($this->session->userdata('user')));		
		$this->load->model('mKategori');
		
		//get session.. 'user' tuh nama session yang udah di set 
//		$this->user	= unserialize(base64_decode($this->session->userdata('user')));		
	}
		public function index($offset=0,$order_column='id_kat', $order_type='asc'){
		if (!empty($this->user)){
			if(empty($offset)) $offset=0;
			if(empty($order_column)) $order_column='id_kat';
			if(empty($order_type)) $order_type='asc';
			//todo : check for valid_barang column
			
			//load data siswa
			$kategori=$this->mKategori->get_paged_list($this->limit,$offset,$order_column,$order_type)->result();
			//generate pagination
			$config['base_url']=site_url('admin/kategori/index/');
			$config['total_rows']=$this->mKategori->count_all();
			$config['per_page']=$this->limit;
			$config['uri_segment']=3;
			$this->pagination->initialize($config);
			$data['pagination']=$this->pagination->create_links();
			
			//generate table data
			$this->table->set_empty("&nbsp;");
			$new_order=($order_type=='asc'?'desc':'asc');
			$this->table->set_heading(
				'ID KATEGORI','NAMA KATEGORI','ACTIONS');
			foreach($kategori as $kat){
					$this->table->add_row($kat->ID_KAT,$kat->NAMA_KAT,anchor('admin/kategori/update/'.$kat->ID_KAT,'update',array('class'=>'update')).' '.anchor('admin/kategori/delete/'.$kat->ID_KAT,'delete',array('class'=>'delete','onclick'=>"return confirm('Apakah Anda yakin ingin menghapus data kategori?')"))
					);
			}
				
				$data['table']=$this->table->generate();
				
				if($this->uri->segment(3)=='delete success') $data['message']='Data berhasil dihapus';
				else if ($this->uri->segment(3)=='add_success') $data['message']='Data berhasil ditambah';
				else $data['message']='';
				//load view
		
			$data['url'] = $this->url;
			$data['header'] = $this->load->view('admin/header/header', $data, true);
			$data['menu'] = $this->load->view('admin/menu/menu', $data, true);
			$data['content'] = $this->load->view('admin/rcontent/kategori/kategori', $data, true);
			$this->load->view('index', $data);
		}
		else
	   {
		 //If no session, redirect to login page
		 redirect('admin/login');
	   }
	}
	
	function add() {
	//set common properties
	$data['title']='tambah merek baru';
	$data['action']=site_url('admin/kategori/add');
	$data['link_back']=anchor('admin/kategori/index/','Back to list of merek',array('class'=>'back'));
	
	$this->_set_rules();
	
	//run valid_barangation
	if($this->form_validation->run()===FALSE){
		$data['message']='';
		//set common properties
		$data['title']='tambah kategori baru';
		$data['message']='';
		$data['kategori']['ID_KAT']='';
		$data['kategori']['NAMA_KAT']='';
		$data['link_back']=anchor('admin/kategori/index','Lihat daftar kategori',array('class'=>'back'));
		$data['url'] = $this->url;
		$data['header'] = $this->load->view('admin/header/header', $data, true);
		$data['menu'] = $this->load->view('admin/menu/menu', $data, true);
		$data['content'] = $this->load->view('admin/rcontent/kategori/katEdit', $data, true);

		$this->load->view('index',$data);
	} else{
		//save data
		$kat=array('id_kat'=>$this->input->post('id_kat'),'nama_kat'=>$this->input->post('nama_kat'));
		$id_kat=$this->mKategori->save($kat);
		
		//set form input nama_brg="id_barang"
		$this->validation->id_kat=$id_kat;
		
		redirect('admin/kategori/index/add_success');
	}
	}
	function update($id_kat){
	//set common properties
	$data['title']='Update kategori';
	
	//set validation properties
		$this->_set_rules();
		$data['action']=('admin/kategori/update/'.$id_kat);
		
		//run validation
		if($this->form_validation->run()===FALSE){
			$data['message']='';
			$data['kategori']=$this->mKategori->get_by_id($id_kat)->row_array();
			
			//set common properties
			$data['title']='Update Kategori';
			$data['message']='';
		}else {
			//save data
			$id_kat=$this->input->post('id_kat');
			$kategori=array('id_kat'=>$this->input->post('id_kat'),'nama_kat'=>$this->input->post('nama_kat'));
			$this->mKategori->update($id_kat,$kategori);
			$data['kategori']=$this->mKategori->get_by_id($id_kat)->row_array();
			
			//set user message
			$data['message']='update kategori berhasil';
		}
		$data['link_back']=anchor('admin/kategori/index/','Lihat daftar kategori',array('class'=>'back'));
		//load view
		$data['url'] = $this->url;
		$data['header'] = $this->load->view('admin/header/header', $data, true);
		$data['menu'] = $this->load->view('admin/menu/menu', $data, true);
		$data['content'] = $this->load->view('admin/rcontent/kategori/katEdit', $data, true);

		$this->load->view('index',$data);
	}
	
	function delete($id_kat){
		//delete siswa
		$this->mKategori->delete($id_kat);
		//redirect to siswa list page
		redirect('admin/kategori/index/delete_success','refresh');
	}
	
	//valid_barangation rules
	function _set_rules(){
		$this->form_validation->set_rules('nama_kat','nama_kat','required|trim');
	}


	
}